/**
 * Created by lizhe on 2018/4/17.
 */
var zTree;
var setting = {
    callback: {
        onClick: zTreeOnClick
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};
var zNodes =[];
var ZtreeId = 0;
var ZtreeName = "";
$(document).ready(function(){
    queryRecommend();

    $("#column_confirm").click(function(){
        var i = document.getElementById("list_i").innerHTML;
        var columnId = "columnId-"+i;
        var column = "column-"+i;
        if(ZtreeId!=0){
            document.getElementById(columnId).innerHTML = ZtreeId;
            document.getElementById(column).value = ZtreeName;
            modalclose("#columnmodal");
        }else{
            modalinfo("请选择");
            modalclose("#columnmodal");
        }
    });
});

//ztree回调
function zTreeOnClick(event, treeId, treeNode) {
    var id = treeNode.id;
    var name = treeNode.name;
    var tId = treeNode.tId;
    ZtreeId = id;
    ZtreeName = name;
}

function queryRecommend(){
    $.ajax({
        url : urladdr + "/web/recommend/list",
        method : "POST",
        data : {
            token:Mytoken
        },
        success : function(data){
            if(data.code == 0){
                $.each(data.data,function(i,date){
                    var id = "id-"+i;
                    var menuid = "columnId-"+i;
                    var title = "column-"+i;
                    document.getElementById(id).innerHTML = date.id;
                    document.getElementById(menuid).innerHTML = date.menuId;
                    document.getElementById(title).value = date.title;
                });
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}

function selectTree(){
    $.ajax({
        url : urladdr + "/web/menu/list",
        method : "GET",
        data : {
            token:Mytoken
        },
        success : function(data){
            if(data.code == 0){
                zNodes.splice(0,zNodes.length);
                $.each(data.data,function(i,date){
                    if(date.height == 1){
                        var d={
                            id:date.id, pId:date.parentId, name:date.title, open:true, iconSkin:"icon01"
                        };
                        zNodes.push(d);
                    }else if(date.height == 2) {
                        d = {
                            id: date.id, pId: date.parentId, name: date.title, open: true, iconSkin: "icon03"
                        };
                        zNodes.push(d);
                    }
                });
                setTimeout(function(){
                    $.fn.zTree.init($("#selectTree"), setting, zNodes);
                },100)
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}

function selectBelong(i){
    document.getElementById("column-"+i).blur();
    showmodal("#columnmodal");
    selectTree();
    document.getElementById("list_i").innerHTML = i;
    ZtreeId = 0;
    ZtreeName = "";
}

function saveRec(i){
    var data={
        id:document.getElementById("id-"+i).innerHTML,
        menuId:document.getElementById("columnId-"+i).innerHTML,
        title:document.getElementById("column-"+i).value
    };
    $.ajax({
        url : urladdr + "/web/recommend/update?token="+Mytoken,
        method : "POST",
        contentType:"application/json; charset=utf-8",
        data : JSON.stringify(data),
        success : function(data){
            if(data.code == 0){
                modalinfo("保存成功");
                queryRecommend();
            }else if(data.code == 401){
                location.href = "index.html"
            }else{
                modalinfo(data.msg);
            }
        }
    });
}
